<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\UserAmountLog;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserAmountLogController extends AdminController
{
    public function index(Content $content)
    {
        return $content
            ->translation($this->translation())
            ->title('金橡果流水')
            ->description($this->description()['index'] ?? trans('admin.list'))
            ->body($this->grid(request('user_id', 0)));
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($user_id)
    {
        return Grid::make(new UserAmountLog(), function (Grid $grid) use ($user_id) {
            $grid->model()
                ->where('user_id', $user_id)
                ->where('amount', '<>', 0)
                ->orderBy('id', 'desc');

            $grid->column('id')->sortable();
            $grid->column('amount');
            $grid->column('balance');
            $grid->column('remark');
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });

            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->disableBatchActions();
            $grid->disableRowSelector();
            $grid->disableFilterButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserAmountLog(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('amount');
            $show->field('balance');
            $show->field('type');
            $show->field('remark');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserAmountLog(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('amount');
            $form->text('balance');
            $form->text('type');
            $form->text('remark');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
